<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Villages' );

$query2 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
if( isset( $row2[ 'faction_id' ] ) ) { 
	$tmp_faction_id = $row2[ 'faction_id' ];
	$query2 = "SELECT farm_support, coal_support, iron_support FROM faction WHERE faction_id = " . $tmp_faction_id;
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$farm_support = $row2[ 'farm_support' ];
	$coal_support = $row2[ 'coal_support' ];
	$iron_support = $row2[ 'iron_support' ];
}
else {
	$farm_support = 0;
	$coal_support = 0;
	$iron_support = 0;
	$tmp_faction_id = -1;
}

$query2 = "SELECT morale FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$morale = $row2[ 'morale' ];

$query = "SELECT * FROM village WHERE ruler_id = " . $_SESSION['ruler_id'] . " ORDER BY name";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
?>
					<div class="blok">
						<a href="javascript:void( null )" onclick="javascript:changeAll( 'minimize' )"><?php transprint( 'minimize all' ); ?></a> / <a href="javascript:void( null )" onclick="javascript:changeAll( 'maximize' )"><?php transprint( 'maximize all' ); ?></a> 
					</div>
					<br />
<?php
$i = 0;
while ( $i < $num ) {
?>
					<table class="blank_dark_top" id="top_<?php print( $i ); ?>">
<?php
	if( mysql_result( $result, $i, 'start_place' ) == 1 ) {
		$title = translate( 'Capital' );
	}
	else {
		if( mysql_result( $result, $i, 'population' ) < 5000 ) {
			$title = translate( 'Outpost' );
		}
		else if( mysql_result( $result, $i, 'population' ) < 10000 ) {
			$title = translate( 'Settlement' );
		}
		else if( mysql_result( $result, $i, 'population' ) < 50000 ) {
			$title = translate( 'Village' );
		}
		else if( mysql_result( $result, $i, 'population' ) < 150000 ) {
			$title = translate( 'Town' );
		}
		else if( mysql_result( $result, $i, 'population' ) < 500000 ) {
			$title = translate( 'City' );
		}
		else {
			$title = translate( 'Metropolis' );
		}
	}
	if( mysql_result( $result, $i, 'hide' ) == 1 ) {
		$hide = " style=\"height: 0px;\"";
		$other = " style=\"display: none;\"";
		$button_title = translate( 'maximize' );
	}
	else {
		$hide = "";
		$other = "";
		$button_title = translate( 'minimize' );
	}
	
	$houses = getMaxPopulation( mysql_result( $result, $i, 'village_id' ) );
	$focus = mysql_result( $result, $i, 'focus' );
?>
						<tr>
							<td><strong><?php print( $title ); ?></strong></td>
							<td align="right">
								<a href="village.php?name=<?php print( mysql_result( $result, $i, "name" ) ); ?>">
									<strong><?php print( mysql_result( $result, $i, "name" ) ); ?></strong>
								</a>
							</td>
							<td>
								<a href="../operations/map.php?x=<?php print( ( mysql_result( $result, $i, "x" ) - 20 ) ); ?>&y=<?php print( ( mysql_result( $result, $i, "y" ) - 20 ) ); ?>">(<?php print( mysql_result( $result, $i, "x" ) ); ?>, <?php print( mysql_result( $result, $i, "y" ) ); ?>)</a>
							</td>
		  					<td align="right"><a class="village_toggle" href="javascript:hide(<?php print( $i ); ?>, <?php print( mysql_result( $result, $i, 'village_id' ) ); ?> )" id="button_<?php print( $i ); ?>"><?php print( $button_title ); ?></a></td>
		  				</tr>
					</table>
					<div class="village_id"><?php print( mysql_result( $result, $i, "village_id" ) ); ?></div>
					<div class="enclosing_dark_middle"<?php print( $hide ); ?>>
						<table<?php print( $other ); ?> class="blank_dark_middle" id="village_<?php print( $i ); ?>">
							<tr>
								<td><?php transprint( 'Population' ); ?></td>
								<td align="right"><strong class="population"><?php print( number_format( mysql_result( $result, $i, "population" ) ) ); ?></strong></td>
								<td><?php transprint( 'Houses' ); ?></td>
								<td align="right"><strong class="houses"><?php print( number_format( $houses ) ); ?></strong></td>
							</tr>
							<tr>
								<td><?php transprint( 'Workers' ); ?></td>
								<td align="right"><strong class="population"><?php print( number_format( mysql_result( $result, $i, "workforce" ) ) ); ?></strong></td>
								<td><?php transprint( 'Jobs occupied' ); ?></td>
								<td align="right"><strong class="jobs"><?php print( number_format( mysql_result( $result, $i, "jobs" ) ) ); ?></strong></td>
							</tr>
<?php
	$food_income = mysql_result( $result, $i, "food_income" );
	if( $food_income < 0 ) {
		$suffix = "-";
	}
	else {
		$suffix = '';
	}
	
	if( $farm_support == 1 ) {
		$food_extra = mysql_result( $result, $i, "food_quality" ) * 0.1;
	}
	else {
		$food_extra = 0;
	}
	if( $coal_support == 1 ) {
		$coal_extra = mysql_result( $result, $i, "coal_quality" ) * 0.1;
	}
	else {
		$coal_extra = 0;
	}
	if( $iron_support == 1 ) {
		$iron_extra = mysql_result( $result, $i, "iron_quality" ) * 0.1;
	}
	else {
		$iron_extra = 0;
	}
	if( $focus == 'housing' ) {
		$coal_extra -= mysql_result( $result, $i, 'coal_quality' ) * 0.1;
		$iron_extra -= mysql_result( $result, $i, 'iron_quality' ) * 0.1;
	}
	elseif( $focus == 'farming' ) {
		$food_extra += mysql_result( $result, $i, 'food_quality' ) * 0.1;
		$coal_extra -= mysql_result( $result, $i, 'coal_quality' ) * 0.1;
		$iron_extra -= mysql_result( $result, $i, 'iron_quality' ) * 0.1;
	}
	elseif( $focus == 'iron' ) {
		$food_extra -= mysql_result( $result, $i, 'food_quality' ) * 0.1;
		$iron_extra += mysql_result( $result, $i, 'iron_quality' ) * 0.1;
	}
	elseif( $focus == 'coal' ) {
		$food_extra -= mysql_result( $result, $i, 'food_quality' ) * 0.1;
		$coal_extra += mysql_result( $result, $i, 'coal_quality' ) * 0.1;
	}
	
	$query2 = "SELECT count(*) FROM village_buildings WHERE building_id = 15 AND village_id = " . mysql_result( $result, $i, "village_id" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	if( $row2[ 'count(*)' ] > 0 ) {
		$food_extra = round( mysql_result( $result, $i, "food_quality" ) * 0.5 + $food_extra );
	}
	
	if( $morale <= 0.2 ) {
		$food_extra -= round( ( mysql_result( $result, $i, "food_quality" ) + $food_extra ) * 0.5 ); 
	}
	else if( $morale <= 0.4 ) {
		$food_extra -= round( ( mysql_result( $result, $i, "food_quality" ) + $food_extra ) * 0.2 ); 
	}
	
	if( $food_extra > 0 ) {
		$added = " (+" . round( $food_extra ) . ")";
	}
	elseif( $food_extra < 0 ) {
		$added = " (" . round( $food_extra ) . ")";
	}
	else {
		$added = "";
	}
	
	print "<tr><td>" . translate( 'Food production' ) . "</td>" .
			"<td align=\"right\"><strong class=\"food\">" . $suffix . number_format( $food_income ) . "</strong></td>" .
			"<td>" . translate( 'quality' ) . "</td><td align=\"right\"><strong class=\"food\">" . mysql_result( $result, $i, "food_quality" ) . "$added</strong></td></tr>\r\n";
	$query2 = "SELECT count(*) FROM village_buildings WHERE building_id = 17 AND village_id = " . mysql_result( $result, $i, "village_id" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	if( $row2[ 'count(*)' ] > 0 ) {
		$iron_extra = round( mysql_result( $result, $i, "iron_quality" ) * 0.5 + $iron_extra );
	}
	
	if( $morale <= 0.2 ) {
		$iron_extra -= round( ( mysql_result( $result, $i, "iron_quality" ) + $iron_extra ) * 0.5 ); 
	}
	else if( $morale <= 0.4 ) {
		$iron_extra -= round( ( mysql_result( $result, $i, "iron_quality" ) + $iron_extra ) * 0.2 ); 
	}
	
	if( $iron_extra > 0 ) {
		$added = " (+" . round( $iron_extra ) . ")";
	}
	elseif( $iron_extra < 0 ) {
		$added = " (" . round( $iron_extra ) . ")";
	}
	else {
		$added = "";
	}
	print "<tr><td>" . translate( 'Iron extraction' ) . "</td><td align=\"right\"><strong class=\"iron\">" . number_format( mysql_result( $result, $i, "iron_income" ) ) . "</strong></td><td>" . translate( 'quality' ) . "</td><td align=\"right\"><strong class=\"iron\">" . mysql_result( $result, $i, "iron_quality" ) . "$added</strong></td></tr>\r\n";
	$query2 = "SELECT count(*) FROM village_buildings WHERE building_id = 19 AND village_id = " . mysql_result( $result, $i, "village_id" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	if( $row2[ 'count(*)' ] > 0 ) {
		$coal_extra = round( mysql_result( $result, $i, "coal_quality" ) * 0.5 + $coal_extra );
	}
	
	if( $morale <= 0.2 ) {
		$coal_extra -= round( ( mysql_result( $result, $i, "coal_quality" ) + $coal_extra ) * 0.5 ); 
	}
	else if( $morale <= 0.4 ) {
		$coal_extra -= round( ( mysql_result( $result, $i, "coal_quality" ) + $coal_extra ) * 0.2 ); 
	}
	
	if( $coal_extra > 0 ) {
		$added = " (+" . round( $coal_extra ) . ")";
	}
	elseif( $coal_extra < 0 ) {
		$added = " (" . round( $coal_extra ) . ")";
	}
	else {
		$added = "";
	}
	print "<tr><td>" . translate( 'Coal extraction' ) . "</td><td align=\"right\"><strong class=\"coal\">" . number_format( mysql_result( $result, $i, "coal_income" ) ) . "</strong></td><td>" . translate( 'quality' ) . "</td><td align=\"right\"><strong class=\"coal\">" . mysql_result( $result, $i, "coal_quality" ) . "$added</strong></td></tr>\r\n";
?>	
							<tr>
								<td><?php transprint( 'Space available' ); ?></td>
								<td align="right"><strong class="space-available"><?php print( mysql_result( $result, $i, "space" ) ); ?></strong></td>
<?php
	$query2 = "SELECT building_id, ammount FROM village_buildings WHERE village_id = " . mysql_result( $result, $i, "village_id" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$num2 = mysql_numrows( $result2 );
	$space_used = 0;
	$p = 0;
	while ( $p < $num2 ) {
		$result3 = mysql_query( "SELECT space FROM buildings WHERE building_id = " . mysql_result( $result2, $p, "building_id" )  ) or die ( "Query failed: " . mysql_error() );
		$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
		if( $row3[ 'space' ] != -1 ) {
			$space_used += mysql_result( $result2, $p, "ammount" ) * $row3[ 'space' ];
		}
		$p ++;
	}

	$result2 = mysql_query( "SELECT building_id FROM village_queue WHERE village_id = " . mysql_result( $result, $i, "village_id" ) . " AND rank = 0 AND time > 0" ) or die ( "Query failed: " . mysql_error() );
	$row = mysql_fetch_array($result2, MYSQL_ASSOC);
	$building_id = $row[ 'building_id' ];
	if( isset( $building_id ) ) {
		$result2 = mysql_query( "SELECT space FROM buildings WHERE building_id = " . $building_id ) or die ( "Query failed: " . mysql_error() );
		$row = mysql_fetch_array( $result2, MYSQL_ASSOC );
		$space_used += $row[ 'space' ];
	}
?>
								<td><?php transprint( 'Space used' ); ?></td>
								<td align="right"><strong class="space-used"><?php print( $space_used ); ?></strong></td>
							</tr>
<?php
	if( $_SESSION[ 'ruler_id' ] == mysql_result( $result, $i, 'loyal_to' ) ) {
		$loyal_towards = '<strong class="green">';
	}
	else {
		$allied = false;
		$query2 = "SELECT faction_id " .
				"FROM faction_member " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		if( isset( $row2[ 'faction_id' ] ) ) {
			$faction_id = $row2[ 'faction_id' ];
			$query2 = "SELECT faction_id " .
					"FROM faction_member " .
					"WHERE ruler_id = " . mysql_result( $result, $i, 'loyal_to' ) . " AND invited = 0 AND requested = 0";
			$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
			if( isset( $row2[ 'faction_id' ] ) && $row2[ 'faction_id' ] == $faction_id ) {
				$allied = true;
			}
		}
		
		if( $allied ) {
			$loyal_towards = '<strong class="blue">';
		}
		else {
			$loyal_towards = '<strong class="red">';
		}
	}
	
	$query2 = "SELECT name " .
			"FROM ruler " .
			"WHERE ruler_id = " . mysql_result( $result, $i, 'loyal_to' );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
	if( isset( $row2[ 'name' ] ) ) {
		$loyal_towards = '<a href="../overview/ruler.php?name=' . $row2[ 'name' ] . '">' . $loyal_towards . $row2[ 'name' ] . '</strong></a>';
	}
	else {
		$loyal_towards = '<strong class="brown">' . translate( 'Independents' ) . '</strong>';
	}
	print( "<tr><td>" . translate( 'Loyalty' ) . "</td><td align=\"right\">" . mysql_result( $result, $i, 'loyalty' ) . '%</td><td>' . translate( 'Loyal towards' ) . '</td><td align="right">' . $loyal_towards . '</td></tr>' );
	
	
	print "</table></div>\r\n";

	print( "<table class=\"blank_dark_bottom\">" );
	$result2 = mysql_query( "SELECT time, name FROM village_queue JOIN buildings ON village_queue.building_id = buildings.building_id WHERE village_id = " . mysql_result( $result, $i, "village_id" ) . " AND rank = 0 AND time > 0" ) or die ( "Query failed: " . mysql_error() );
	$row = mysql_fetch_array($result2, MYSQL_ASSOC);
	if( isset( $row[ 'time' ] ) ) {
		$building = translate( $row[ 'name' ] );
		$time = $row[ 'time' ];
		if( $time == 1 ) {
			$days = translate( 'day' );
		}
		else {
			$days = translate( 'days' );
		}
	}
	else {
		$result2 = mysql_query( "SELECT count(*) FROM village_queue WHERE village_id = " . mysql_result( $result, $i, "village_id" ) ) or die ( "Query failed: " . mysql_error() );
		$row = mysql_fetch_array($result2, MYSQL_ASSOC);
		if( $row[ 'count(*)' ] == 0 ) {
			$building = "<a title=\"" . translate( 'Give new instructions' ) . "\" href=\"village.php?name=" . mysql_result( $result, $i, "name" ) . "\"><strong>" . translate( 'waiting for instructions' ) . "</strong></a>";
			$time = "";
			$days = "";
		}
		else {
			$building = translate( 'Preparing for construction' );
			$time = "";
			$days = "";
		}
	}
	print "<tr><td>" . translate( 'Currently building' ) . "</td><td align=\"right\">$building</td><td align=\"right\">$time $days</td><td align=\"right\"></td></tr>\r\n";
	print "</table>\r\n";
	
	$query2 = "SELECT name, ruler_id, scout, infantry, cavalry, artillery FROM army " .
			"WHERE x = " . mysql_result( $result, $i, "x" ) . " AND y = " . mysql_result( $result, $i, "y" ) . " AND travel = 0";	
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$num2 = mysql_numrows( $result2 );
	$p = 0;
	if( $num2 > 0 ) {
		print( "<table class=\"blank_dark_bottom\">\r\n" );
		print( "<tr><td id=\"village_army\" align=\"left\">" . translate( 'Armies' ) . " " );
		while ( $p < $num2 ) {
			if( $p > 0 ) {
				print( ", " );
			}
			if( mysql_result( $result2, $p, "ruler_id" ) == $_SESSION[ 'ruler_id' ] ) {
				print( "<a href=\"../armies/army.php?name=" . mysql_result( $result2, $p, "name" ) . "\"><strong id=\"green\">" . mysql_result( $result2, $p, "name" ) . "</strong></a>" .
						" (" . translate( 'size' ) . ": " . number_format( mysql_result( $result2, $p, "scout" ) + mysql_result( $result2, $p, "infantry" ) + mysql_result( $result2, $p, "cavalry" ) + mysql_result( $result2, $p, "artillery" ) ) . ")" );
			}
			else {
				$result3 = mysql_query( "SELECT faction_id FROM faction_member WHERE ruler_id = " . mysql_result( $result2, $p, "ruler_id" ) . " AND requested = 0 AND invited = 0" ) or die ( "Query failed: " . mysql_error() );
				$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
				$result4 = mysql_query( "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND requested = 0 AND invited = 0" ) or die ( "Query failed: " . mysql_error() );
				$row4 = mysql_fetch_array($result4, MYSQL_ASSOC);
				if( isset( $row3[ 'faction_id' ] ) && isset( $row4[ 'faction_id' ] ) && $row4[ 'faction_id' ] == $row3[ 'faction_id' ] ) {
					print( "<strong class=\"blue\">" . mysql_result( $result2, $p, "name" ) . "</strong>" .
							" (" . translate( 'size' ) . ": " . number_format( mysql_result( $result2, $p, "scout" ) + mysql_result( $result2, $p, "infantry" ) + mysql_result( $result2, $p, "cavalry" ) + mysql_result( $result2, $p, "artillery" ) ) . ")" );
				}
				else {
					print( "<strong class=\"red\">" . mysql_result( $result2, $p, "name" ) . "</strong>" .
							" (" . translate( 'size' ) . ": " . number_format( mysql_result( $result2, $p, "scout" ) + mysql_result( $result2, $p, "infantry" ) + mysql_result( $result2, $p, "cavalry" ) + mysql_result( $result2, $p, "artillery" ) ) . ")" );
				}
			}
			$p ++;
		}
		print( "</td></tr></table>\r\n" );
	}

	$i ++;
	if( $i < $num ) {
		print "<br/>\r\n";
	}
}
?>
					<script language="JavaScript" type="text/javascript">
						function showTable() {
							var el = this.getEl();
							var table = YAHOO.util.Dom.getElementsByClassName( 'blank_dark_middle', 'table', el );
							if( table[0] ) {
								YAHOO.util.Dom.setStyle( table[0], "display", "block" );
							}
						}
			
						function hide( number, id ) {
							var bottom = document.getElementById( "village_" + number );
							var top = document.getElementById( "top_" + number );
							var link = document.getElementById( "button_" + number );
							
							if( bottom.parentNode.style.height == "0px" ) {
								var anim = new YAHOO.util.Anim( bottom.parentNode, {
									height: { to: 138 } 
								}, 1, YAHOO.util.Easing.easeBoth ); 
								anim.onComplete.subscribe( showTable );
								anim.animate();
								link.innerHTML = "<?php transprint( 'minimize' ); ?>";
								YAHOO.util.Connect.asyncRequest( 'GET', 'village_state.php?' + "village_id=" + id + "&set=0", stateCallback, null );
							}
							else {
								var anim = new YAHOO.util.Anim( bottom.parentNode, {
									height: { to: 0 } 
								}, 1, YAHOO.util.Easing.easeBoth ); 
								anim.animate();
								link.innerHTML = "<?php transprint( 'maximize' ); ?>";
								YAHOO.util.Connect.asyncRequest( 'GET', 'village_state.php?' + "village_id=" + id + "&set=1", stateCallback, null );
								YAHOO.util.Dom.setStyle( bottom, "display", "none" );
							}
						}
						
						var stateCallback = {
							timeout: 5000
						};
						
						function changeAll( to ) {
							var villages = YAHOO.util.Dom.getElementsByClassName( 'blank_dark_middle', 'table' );
							var links = YAHOO.util.Dom.getElementsByClassName( 'village_toggle', 'a' );
							var village_ids = YAHOO.util.Dom.getElementsByClassName( 'village_id', 'div' );
							
							if( to == 'maximize' ) { 
								YAHOO.util.Connect.asyncRequest( 'GET', 'village_state.php?' + "village_id=all&set=0", stateCallback, null );
							}
							else {
								YAHOO.util.Connect.asyncRequest( 'GET', 'village_state.php?' + "village_id=all&set=1", stateCallback, null );
							}
							
							for( i = 0; i < villages.length; i ++ ) {
								if( ( villages[i].parentNode.style.height == "0px" || villages[i].parentNode.style.height == "" ) && to == 'maximize' ) { 
									var anim = new YAHOO.util.Anim( villages[i].parentNode, {
										height: { to: 138 } 
									}, 1, YAHOO.util.Easing.easeBoth );
									anim.onComplete.subscribe( showTable ); 
									anim.animate();
									links[i].innerHTML = "<?php transprint( 'minimize' ); ?>";
								}
								else if( ( villages[i].parentNode.style.height == "138px" || villages[i].parentNode.style.height == "" ) && to == 'minimize' ) {
									var anim = new YAHOO.util.Anim( villages[i].parentNode, {
										height: { to: 0 } 
									}, 1, YAHOO.util.Easing.easeBoth ); 
									anim.animate();
									links[i].innerHTML = "<?php transprint( 'maximize' ); ?>";
									YAHOO.util.Dom.setStyle( villages[i], "display", "none" );
								}
							}
						}
					</script>
<?php
aos_footer();
?>